//
//  LBFadeImageLoopView.m
//  LBFadeImageLoopView
//
//  Created by Apple on 2021/9/19.
//

#import "LBFadeImageLoopView.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImage.h>

@interface LBFadeImageLoopView ()

///图片视图
@property (nonatomic, strong) UIImageView *imgView;

@property (nonatomic, strong) NSTimer *timer;

/// 网络图片URL数组
@property (nonatomic, strong) NSArray <NSString *> *urls;

///image 数组
@property (nonatomic, strong) NSArray <UIImage *> *images;

///当前展示的序号
@property (nonatomic, assign) NSInteger index;

@end

@implementation LBFadeImageLoopView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.stayTimeInterval = 3;
        self.fadeTime = 1;
        [self setUpUI];
        [self setUpConstraints];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)];
        [self addGestureRecognizer:tap];
    }
    return self;
}

- (void)setUpUI
{
    [self addSubview:self.imgView];
}

- (void)setUpConstraints
{
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
}

#pragma mark - action
- (void)click
{
    if (self.delegate &&
        [self.delegate respondsToSelector:@selector(fadeLoopView:clickItemAtIndex:)]) {
        [self.delegate fadeLoopView:self clickItemAtIndex:self.index];
    }
}

#pragma mark - public

- (void)loadWithimagUrls:(NSArray<NSString *> *)urls
{
    if (urls == self.urls) {
        return;
    }
    self.index = 0;
    _urls = urls;
    [self displayUrlImage];
    if (_timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
    if (urls.count < 2) {
        return;
    }
    __weak typeof(self) weakSelf = self;
    self.timer = [NSTimer timerWithTimeInterval:self.stayTimeInterval + self.fadeTime repeats:YES block:^(NSTimer * _Nonnull timer) {
        [weakSelf displayUrlImage];
    }];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)loadWithImages:(NSArray<UIImage *> *)images
{
    if (images == self.images) {
        return;
    }
    
    self.index = 0;
    self.images = images;
    [self displayImages];
    
    if (_timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
    if (images.count < 2) {
        return;
    }
    __weak typeof(self) weakSelf = self;
    self.timer = [NSTimer timerWithTimeInterval:self.stayTimeInterval + self.fadeTime repeats:YES block:^(NSTimer * _Nonnull timer) {
        [weakSelf displayImages];
    }];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)pause
{
    [self.timer setFireDate:[NSDate distantFuture]];
}

- (void)reStartTimer
{
    [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:0]];
}

- (void)stopTimer
{
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
}

#pragma mark - private
-(void)displayUrlImage {
    if (self.urls.count == 0) {
        return;
    }
    self.index ++;
    
    if (self.index >= [self.urls count]) {
       self.index = 0;
    }
    CATransition *animation = [CATransition animation];
    animation.duration = 1.2f ;
    animation.fillMode = kCAFillModeForwards;
    animation.type = kCATransitionFade;
    
    [self.imgView.layer addAnimation:animation forKey:@"animationID"];
    NSString *url = self.urls[self.index];
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:url]];
}

- (void)displayImages
{
    if (self.images.count == 0) {
        return;
    }
    self.index ++;
    
    if (self.index >= [self.images count]) {
       self.index = 0;
    }
    CATransition *animation = [CATransition animation];
    animation.duration = 1.2f ;
    animation.fillMode = kCAFillModeForwards;
    animation.type = kCATransitionFade;
    
    [self.imgView.layer addAnimation:animation forKey:@"animationID"];
    UIImage *image = self.images[self.index];
    self.imgView.image = image;
}

#pragma mark - lazy load

- (UIImageView *)imgView
{
    if (!_imgView) {
        _imgView = [[UIImageView alloc] init];
    }
    return _imgView;
}

@end
